// HSS.Logging.LogService.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       LogService.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Logging
{
	#region Using Directives
	using System;
	using System.ServiceModel;
	using System.ServiceProcess;
	#endregion

	#region LogService
	/// <summary>
	/// LogService
	/// </summary>
	public partial class LogService : ServiceBase
	{
		#region Fields
		/// <summary>
		/// The ServiceHost that is hosting the LogServer.
		/// </summary>
		public ServiceHost serviceHost = null;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructor
		/// </summary>
		public LogService()
		{
			InitializeComponent();
		}
		#endregion

		#region Service Overrides
		/// <summary>
		/// Start the service
		/// </summary>
		/// <param name="args">Does not use any commandline arguments.</param>
		protected override void OnStart(string[] args)
		{
			if (serviceHost != null)
			{
				try { serviceHost.Close(); }
				catch { }
			}
			serviceHost = new ServiceHost(typeof(LogServer), new Uri(Log.HSSLogServerURI));
			serviceHost.AddServiceEndpoint(typeof(ILogServer), this.GetBinding(), "");
			serviceHost.Open();
		}
		/// <summary>
		/// Stop the service
		/// </summary>
		protected override void OnStop()
		{
			if (AutoTruncateService.IsStarted)
				AutoTruncateService.Stop();

			if (serviceHost != null)
			{
				try { serviceHost.Close(); }
				catch { }
				serviceHost = null;
			}
		}
		/// <summary>
		/// Shutdown the service
		/// </summary>
		protected override void OnShutdown()
		{
			if (AutoTruncateService.IsStarted)
				AutoTruncateService.Stop();

			if (serviceHost != null)
			{
				try { serviceHost.Close(); }
				catch { }
				serviceHost = null;
			}
		}
		#endregion

		private NetNamedPipeBinding GetBinding()
		{
			var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
			binding.MaxReceivedMessageSize = 0x7fffffffL;
			SetReaderQuotas(binding.ReaderQuotas);
			return binding;
		}
		private static void SetReaderQuotas(System.Xml.XmlDictionaryReaderQuotas readerQuotas)
		{
			readerQuotas.MaxArrayLength = 0x7fffffff;
			readerQuotas.MaxBytesPerRead = 0x7fffffff;
			readerQuotas.MaxDepth = 0x7fffffff;
			readerQuotas.MaxNameTableCharCount = 0x7fffffff;
			readerQuotas.MaxStringContentLength = 0x7fffffff;
		}
	}
	#endregion
}